package com.example.dong.clients.factory;

import com.example.dong.clients.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

/**
 * 工厂创建工具
 * @author dzh
 * @date 2022/12/10 13:47
 */
@Slf4j
public abstract class FeignClientFallbackProxyFactory {
    public static <T> T createFallbackProxy(Class<T> clazz, Throwable cause) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> {
            log.error("fallback {} {}", method, cause);
            return Result.FALLBACK;
        });
        return (T) enhancer.create();
    }
}
